package com.lf.yapinmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lf.yapinmall.common.utils.PageUtils;
import com.lf.yapinmall.common.utils.Query;
import com.lf.yapinmall.product.dao.BrandDao;
import com.lf.yapinmall.product.dao.CategoryBrandRelationDao;
import com.lf.yapinmall.product.dao.CategoryDao;
import com.lf.yapinmall.product.entity.BrandEntity;
import com.lf.yapinmall.product.entity.CategoryBrandRelationEntity;
import com.lf.yapinmall.product.entity.CategoryEntity;
import com.lf.yapinmall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("categoryBrandRelationServiceImpl")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    BrandDao brandDao;

    @Autowired
    CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> list(Long brandId) {
        return this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
    }

    @Override
    public int saveDetails(CategoryBrandRelationEntity categoryBrandRelationEntity) {

        int result = 0;
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<CategoryBrandRelationEntity>();
        queryWrapper.eq("brand_id",categoryBrandRelationEntity.getBrandId()).eq("catelog_id",categoryBrandRelationEntity.getCatelogId());
        if(baseMapper.selectOne(queryWrapper) == null){
            Long brandId = categoryBrandRelationEntity.getBrandId();
            Long catelogId = categoryBrandRelationEntity.getCatelogId();
            //1、查询详细名字
            BrandEntity brandEntity = brandDao.selectById(brandId);
            CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
            categoryBrandRelationEntity.setBrandName(brandEntity.getName());
            categoryBrandRelationEntity.setCatelogName(categoryEntity.getName());
            this.save(categoryBrandRelationEntity);
            result = 1;
        }
        return result;
    }

    @Override
    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCategory(catId,name);
    }
}
